/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.modelo;

import dadosUsuarioLogado.UsuarioLogado;
import java.io.Serializable;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author gilberto
 */
public class EmailUtils implements Serializable{

    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String HOSTNAME = "smtp.googlemail.com";
    private static final String DESTINATARIO = "gmc.beto40@gmail.com";
    private static final String PASSWORD = "analisegmc";
    private static final String POP3PORT = "995";
    private static final String SMTPORT = "465";

    public static void enviaEmail(Mensagem mensagem) throws MessagingException {
        Properties props = System.getProperties();
        props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.pop3.socketFactory.fallback", "false");
        props.setProperty("mail.pop3.port", POP3PORT);
        props.setProperty("mail.pop3.socketFactory.port", POP3PORT);
        Session session = Session.getDefaultInstance(props, null);
        URLName urln = new URLName("pop3", "pop.gmail.com", 995, null,
                DESTINATARIO, PASSWORD);
        Store store = session.getStore(urln);
        Folder inbox = null;

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        props = System.getProperties();
        props.setProperty("mail.smtp.host", HOSTNAME);
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", SMTPORT);
        props.setProperty("mail.smtp.socketFactory.port", SMTPORT);
        props.put("mail.smtp.auth", "true");

        session = Session.getInstance(props, new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(DESTINATARIO, PASSWORD);
            }
        });

//        se estiver algum usuario logado envia os seus dados
        Operador usu = UsuarioLogado.retornaUsuarioLogado();
        String dados = "Usu�rio sem cadastro na sistema!!";
        if (usu != null) {
            dados = "Nome completo: " + usu.getLogin() + "\n"
                    + "Nome de Login: " + usu.getLogin() + "\n"
                    + "Perfil do usuário: " + usu.getIdEcfFuncionario().getNivelAutorizacao().toString();
        }
        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(DESTINATARIO));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(DESTINATARIO, false));
        msg.setSubject(mensagem.getTitulo());
        msg.setText(mensagem.getMensagem() + "\n\nMensagem enviado por: " + mensagem.getDestino() + "\n\n"
                + dados);
        msg.setSentDate(new Date());
        Transport.send(msg);
    }
}